

import javafx.animation.FadeTransition;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.text.TextFlow;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.util.Duration;

/**
 * @Description 登录Demo
 * @Author AF
 * @Version 1.0.0
 * @Date 2024/1/29
 */
public class LoginDemo extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {

        Label name = new Label("账号:");
        name.setFont(Font.font(14));
        Label password = new Label("密码:");
        password.setFont(Font.font(14));

        TextField nameField = new TextField();
        nameField.setUserData("admin"); // 设置用户数据，相当于context,只可以赋值一次
        nameField.setTooltip(new Tooltip("请输入用户名"));
//        nameField.getProperties().put("username","admin"); // 可以多次赋值

        PasswordField passwordField = new PasswordField();
        passwordField.setUserData("admin");
        passwordField.setTooltip(new Tooltip("请输入密码"));
//        passwordField.getProperties().put("password","admin");

        Button login = new Button("登录");
        Button clear = new Button("清除");

        // 网格布局
        GridPane gridPane = new GridPane();
        gridPane.setStyle("-fx-background-color: #FFF5EE");
        gridPane.setAlignment(Pos.CENTER);
        gridPane.add(name, 0, 0);
        gridPane.add(nameField, 1, 0);
        gridPane.add(password, 0, 1);
        gridPane.add(passwordField, 1, 1);
        gridPane.add(clear, 0, 2);
        gridPane.add(login, 1, 2);
        gridPane.setHgap(5);
        gridPane.setVgap(17);

        GridPane.setMargin(login, new Insets(0, 0, 0, 120));

        Scene scene = new Scene(gridPane);
        primaryStage.setScene(scene);
        primaryStage.setTitle("登录");
        primaryStage.setWidth(400);
        primaryStage.setHeight(500);
        primaryStage.setResizable(false);
        primaryStage.show();

        clear.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                nameField.setText("");
                passwordField.setText("");
            }
        });

        login.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                String username = nameField.getText();
                String password = passwordField.getText();

                if (!username.equals(nameField.getUserData()) || !password.equals(passwordField.getUserData())) {
                    // 弹出错误框
                    DialogPane dialogPane = new DialogPane();
                    dialogPane.setContentText("账号或密码不正确");

                    // 动画效果,闪一下
                    FadeTransition fade = new FadeTransition();
                    fade.setDuration(Duration.seconds(1.5));
                    fade.setNode(gridPane);
                    fade.setFromValue(0);
                    fade.setToValue(1);
                    fade.play();

                    Scene scene1 = new Scene(dialogPane);
                    Stage stage = new Stage();
                    stage.initOwner(primaryStage);
                    stage.initModality(Modality.WINDOW_MODAL);
                    stage.initStyle(StageStyle.DECORATED);
                    stage.setScene(scene1);
                    stage.setTitle("错误提示");
                    stage.show();
                } else {
                    // 登录成功
                    primaryStage.close();
                    new SuccessWindow(username, password);
                }
            }
        });
    }
}


class SuccessWindow {
    private final Stage stage = new Stage();

    public SuccessWindow(String username, String password) {
        Text text = new Text("用户名=" + username + ", 密码=" + password);

        BorderPane borderPane = new BorderPane();
        borderPane.setStyle("-fx-background-color: #ffC0CB");
        borderPane.setCenter(text);

        Scene scene = new Scene(borderPane);
        stage.setScene(scene);
        stage.setTitle("登录成功");
        stage.setWidth(500);
        stage.setHeight(700);
        stage.show();
    }
}